/**
 * Created by Administrator on 2018/4/2.
 */

var js_config = {};

layui.use(['form'], function () {
    var form = layui.form
        , layer = layui.layer;

    /**
     * 页面初始化，数据渲染
     */
    function initPage() {
        var token = sessionStorage.access_token;
        if(token==null){
            againToken();
            return;
        }
        var vue = new Vue({
            el: '#index_div',
            data: {
                is_show: 0,  //是否显示弹出层   0：不显示  1：领取挑战卡
                user_info: {},    //用户信息
                config_info: {},   //用户配置信息
                js_config: {},   //微信JS-SDK配置信息
            },
            watch: {},
            methods: {
                onClosePopupVue: function (e) {
                    onCloseClickPopup(e.currentTarget);
                },
                goWithdrawalRecord:function(){
                  window.location.href = 'withdrawal_record.html';
                },
                onWithdrawalPopup: function () {    //提现弹出框
                    $('#withdrawal_pop_up').show();
                    $('html,body').addClass('ovfHiden'); //使网页可滚动
                    initPopupHeight();
                },
                onChallengeCardPopup: function () { //领取挑战卡弹出框
                    $('#challenge_card_pop_up').show();
                    $('html,body').addClass('ovfHiden'); //使网页可滚动
                    initPopupHeight();
                },
                onPublicNoPopup: function () {  //公众号弹出框
                    $('#public_no_pop_up').show();
                    $('html,body').addClass('ovfHiden'); //使网页可滚动
                    initPopupHeight();
                },
                onCustomerPopup: function () {  //联系客服弹出框
                    $('#customer_pop_up').show();
                    $('html,body').addClass('ovfHiden'); //使网页可滚动
                    initPopupHeight();
                },
                onShowCandLog: function () {
                    window.location.href = 'my_challenge.html?type=1';
                },
                onWithdrawal: function () {    //进行提现
                    var money = parseInt($('input[name="money"]').val());
                    console.log(money);

                    if (!money) {
                        layer.msg('提现金额不能为空');
                        return;
                    }
                    if (!isPositiveInteger(money)) {
                        layer.msg('提现金额为正整数');
                        return;
                    }
                    if (money < parseInt(this.config_info.lowest_limit) || money > parseInt(this.config_info.highest_limit)) {
                        layer.msg('单次最低提现额度为' + this.config_info.lowest_limit + '，最高为' + this.config_info.highest_limit);
                        return;
                    }

                    var data = {
                        token: token,
                        money: money
                    }
                    $.post("/index/withdrawal/ajax_withdrawal", data, function (result) {
                        onSelfClosePopup('withdrawal_pop_up');
                        console.info(result);
                        if (result.code == -1) {   //失败
                            layer.msg(result.msg);
                        }
                        if (result.code == 0) { //Token失效
                            againToken();
                            return;
                        }
                        if (result.code == 1) { //成功
                            layer.msg(result.msg);

                            vue.user_info.balance = vue.user_info.balance - money;
                        }

                        if (result.code == 2) {
                            layer.msg(result.msg);
                        }
                    });
                    //layer.msg('提现金额为' + money, {
                    //    time: 20000, //20s后自动关闭
                    //    btn: ['确定', '取消'],
                    //    shade: [0.1, '#FFFFFF'],
                    //    yes: function () {
                    //
                    //    }
                    //});

                }

            },
            filters: {
                integral: function (value) {
                    return parseInt(value);
                },
                poundage: function (value) {
                    return parseFloat(value) * 100;
                }
            }
        });

        function initShowPopup() {

            var is_show = getQueryString('is_show') == null ? 0 : getQueryString('is_show');
            vue.is_show = is_show;
            if (vue.is_show == 1) {
                vue.onChallengeCardPopup();
            }
            if (vue.is_show == 2) {
                vue.onPublicNoPopup();
                initPopupHeight();
            }
        }

        /**
         * 初始化JS-SDK配置信息
         */
        function initJsConfig() {

            var data = {
                token: token,
                url:window.location.href
            }

            $.post("/index/config/ajax_get_wechat_js", data, function (result) {
                //if (result.code == -1) {   //失败
                //    layer.msg(result.msg);
                //    return;
                //}
                if (result.code == 0) { //Token失效
                    againToken();
                    return;
                }
                if (result.code == 1) { //成功
                    vue.js_config = result.data;
                    js_config = vue.js_config;
                    startJsSDK();
                }

            });
        }



        initUserInfo(vue, token);
         initConfigInfo(vue, token);
        initShowPopup();
        initJsConfig();
    }

    /**
     * 初始化弹出层位置
     */
    function initPopupHeight() {
        var wH = $('.withdrawal-box').outerHeight();
        $('.withdrawal-box').css('marginTop', (winH - wH) * 0.45 + 'px');

        var pH = $('.public-no-box').outerHeight();
        //console.log(pH);
        $('.public-no-box').css('marginTop', (winH - 400) * 0.45 + 'px');

        var cH = $('.customer-box').outerHeight();
        $('.customer-box').css('marginTop', (winH - cH) * 0.45 + 'px');

    };

    /**
     * 初始化完成加载函数
     */
    $(function () {
        initGlobalPage();
        initPage();
    })
});


